/*
 * TextDialog.java
 *
 * Created on Oct 5, 2011, 10:46:45 PM
 */
package com.photoexpert;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.event.ItemEvent;
import java.awt.image.BufferedImage;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;

/**
 *
 * @author PANYIXIN
 */
class TextDialog extends javax.swing.JDialog {

    /** Creates new form TextDialog */
    TextDialog() {
        super(App.Instance(), true);
        initComponents();
        setLocation(200, 200);
        setFont();
        setVisible(true);
    }

    private void setFont() {
        String fontName = (String)(_fontName.getSelectedItem());
        int style = 0;
        if(_bold.isSelected())
            style |= Font.BOLD;
        if(_italic.isSelected())
            style |= Font.ITALIC;
        String s = _size.getModel().getValue().toString();
        int size = Integer.parseInt(s);
        _font = new Font(fontName, style, size);
        _preview.setFont(_font);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        _textLabel = new javax.swing.JLabel();
        _text = new javax.swing.JTextField();
        _colorButton = new javax.swing.JButton();
        _previewLabel = new javax.swing.JLabel();
        _preview = new javax.swing.JLabel();
        _cancel = new javax.swing.JButton();
        _ok = new javax.swing.JButton();
        _fontName = new JComboBox(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
        _bold = new javax.swing.JCheckBox();
        _italic = new javax.swing.JCheckBox();
        _size = new javax.swing.JSpinner();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Draw text");

        _textLabel.setText("Text:");

        _text.setText("TEXT");
        _text.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                _textKeyReleased(evt);
            }
        });

        _colorButton.setText("Color");
        _colorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _colorButtonActionPerformed(evt);
            }
        });

        _previewLabel.setText("Preview:");

        _preview.setFont(new java.awt.Font("Courier New", 0, 18));
        _preview.setText("TEXT");

        _cancel.setText("Cancel");
        _cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _cancelActionPerformed(evt);
            }
        });

        _ok.setText("OK");
        _ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _okActionPerformed(evt);
            }
        });

        _fontName.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                _fontNameItemStateChanged(evt);
            }
        });

        _bold.setText("bold");
        _bold.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                _boldStateChanged(evt);
            }
        });

        _italic.setText("italic");
        _italic.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                _italicStateChanged(evt);
            }
        });

        _size.setModel(new javax.swing.SpinnerNumberModel(18, 6, 72, 1));
        _size.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                _sizeStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(_preview, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(_ok, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_cancel))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(_textLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(_text, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(_colorButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(_fontName, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(_size, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(_bold)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(_italic))))
                    .addComponent(_previewLabel, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(_textLabel)
                    .addComponent(_text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(_colorButton)
                    .addComponent(_fontName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(_size, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(_bold)
                    .addComponent(_italic))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(_previewLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(_preview, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(_cancel)
                    .addComponent(_ok))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void _cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__cancelActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event__cancelActionPerformed

    private void _okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__okActionPerformed
        Graphics g = Implementor.Instance().getImage().createGraphics();
        FontMetrics metrics = g.getFontMetrics(_font);
        int width = metrics.stringWidth(_text.getText()) + 2;
        int height = metrics.getHeight() + 2;
        g.dispose();
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        int rgb = 0x00000000;
        for(int x = 0; x < width; ++x)
            for(int y = 0; y < height; ++y)
                img.setRGB(x, y, rgb);
        g = img.createGraphics();
        g.setFont(_font);
        g.setColor(_color);
        g.drawString(_text.getText(), 1, height - 1);
        g.dispose();
        MPaster.Instance().startPaste(img);
        Presentor.Instance().setMouseMode(Presentor.MOUSE_MODES.PASTING);
        setVisible(false);
        dispose();
    }//GEN-LAST:event__okActionPerformed

    private void _colorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__colorButtonActionPerformed
        Color c = JColorChooser.showDialog(null, "Photo Expert", _color);
        if(c != null)
        {
            _color = c;
            _preview.setForeground(_color);
        }
    }//GEN-LAST:event__colorButtonActionPerformed

    private void _textKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event__textKeyReleased
        _preview.setText(_text.getText());
    }//GEN-LAST:event__textKeyReleased

    private void _fontNameItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event__fontNameItemStateChanged
        if (evt.getStateChange() != ItemEvent.SELECTED)
            return;
        setFont();
    }//GEN-LAST:event__fontNameItemStateChanged

    private void _sizeStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event__sizeStateChanged
        setFont();
    }//GEN-LAST:event__sizeStateChanged

    private void _boldStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event__boldStateChanged
        setFont();
    }//GEN-LAST:event__boldStateChanged

    private void _italicStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event__italicStateChanged
        setFont();
    }//GEN-LAST:event__italicStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox _bold;
    private javax.swing.JButton _cancel;
    private javax.swing.JButton _colorButton;
    private javax.swing.JComboBox _fontName;
    private javax.swing.JCheckBox _italic;
    private javax.swing.JButton _ok;
    private javax.swing.JLabel _preview;
    private javax.swing.JLabel _previewLabel;
    private javax.swing.JSpinner _size;
    private javax.swing.JTextField _text;
    private javax.swing.JLabel _textLabel;
    // End of variables declaration//GEN-END:variables
    private Font        _font = null;
    private Color       _color = Color.BLACK;
}
